package yartiss.gui.main;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;

import yartiss.gui.view.BenchmarkView;
import yartiss.gui.view.TasksetsGeneratorView;
import yartiss.gui.view.TimelineView;
import yartiss.service.viewmodel.BenchmarkViewModel;
import yartiss.service.viewmodel.TaskSetsGenViewModel;
import yartiss.service.viewmodel.TimeLineViewModel;

@SuppressWarnings("serial")
public class DesktopMain extends JFrame{
	
	private final TimelineView chronogramView = new TimelineView();
	private final TimeLineViewModel chronogramViewModel = new TimeLineViewModel();
	private final BenchmarkViewModel comparingViewModel = new BenchmarkViewModel();
	private final BenchmarkView comparingView= new BenchmarkView();
	private final TasksetsGeneratorView genView= new TasksetsGeneratorView();
	private final TaskSetsGenViewModel genViewModel = new TaskSetsGenViewModel();
	
	public DesktopMain(){
		super("YARTISS-");
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension d = new Dimension(screenSize);
		d.width  *= (5.0 / 6.0);
		d.height *= (5.0 / 6.0);
		setBounds((screenSize.width - d.width) / 2, (screenSize.height - d.height) / 2, d.width, d.height);
		setPreferredSize(d);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		chronogramViewModel.setView(chronogramView);
		setContentPane((JComponent) chronogramViewModel.getView());
		 
		setJMenuBar(buildMenuBar());
	}
	
	private JMenuBar buildMenuBar(){
		JMenuBar menuBar = new JMenuBar();
		JMenu fileMenu = new JMenu("File");
		JMenuItem quit = new JMenuItem("Quit");
		quit.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		fileMenu.add(quit);
		
		JMenu viewMenu = new JMenu("View");
		JMenuItem chronogramViewMenu = new JMenuItem("Chronogram");
		chronogramViewMenu.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent e) {
				chronogramViewModel.setView(chronogramView);
				setContentPane((JComponent) chronogramViewModel.getView());
				revalidate();
			}
		});
		viewMenu.add(chronogramViewMenu);
		
		JMenuItem comparingViewMenu = new JMenuItem("Comparing");
		comparingViewMenu.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent e) {
				comparingViewModel.setView(comparingView);
				setContentPane((JComponent) comparingViewModel.getView());
				revalidate();
			}
		});
		viewMenu.add(comparingViewMenu);
		
		JMenu tools = new JMenu("Tools");
		JMenuItem generate = new JMenuItem("Task sets generator");
		generate.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent e) {
				genViewModel.setView(genView);
				setContentPane((JComponent) genViewModel.getView());
				revalidate();
			}
		});
		tools.add(generate);
		
		JMenu help = new JMenu("Help");
		
		JMenuItem doc = new JMenuItem("Documentation");
		doc.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent e) {
				//TODO
			}
		});
		help.add(doc);
		
		JMenuItem about = new JMenuItem("About");
		about.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent e) {
				//TODO
			}
		});
		help.add(about);
		
		
		menuBar.add(fileMenu);
		menuBar.add(viewMenu);
		menuBar.add(tools);
		menuBar.add(help);
		return menuBar;
	}
	
	public static void main(String[] args) throws FileNotFoundException, Exception {

		UIManager.setLookAndFeel(
				UIManager.getCrossPlatformLookAndFeelClassName());

		 DesktopMain main = new DesktopMain();
		 main.setVisible(true);
	}
}
